package com.cryptoneedle.center.doris.service.doris;

import cn.hutool.v7.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.bubbles.engine.data.core.repository.BaseRepository;
import com.bubbles.engine.data.core.service.BaseService;
import com.cryptoneedle.center.common.entity.doris.DorisCatalog;
import com.cryptoneedle.center.common.key.CatalogKey;
import com.cryptoneedle.center.common.repository.doris.DorisCatalogRepository;
import com.cryptoneedle.center.doris.repository.metadata.DorisMetadataRepository;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>description: Doris Catalog </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@DS("pg")
@Slf4j
public class DorisCatalogService extends BaseService<DorisCatalog, CatalogKey> {

    @Resource
    private DorisCatalogService dorisCatalogService;

    @Resource
    private DorisCatalogRepository dorisCatalogRepository;

    @Resource
    private DorisMetadataRepository dorisMetadataRepository;


    @Override
    public BaseRepository<DorisCatalog, CatalogKey> getRepository() {
        return this.dorisCatalogRepository;
    }


    @Transactional
    public List<DorisCatalog> list() {
        return dorisCatalogRepository.list();
    }

    @Transactional
    public void addList(List<DorisCatalog> list) {
        dorisCatalogRepository.saveAll(list);
    }

    @Transactional
    public void saveList(List<DorisCatalog> list) {
        dorisCatalogRepository.saveAll(list);
    }

    @Transactional
    public void deleteList(List<DorisCatalog> list) {
        dorisCatalogRepository.deleteAll(list);
    }

    public List<DorisCatalog> syncDorisCatalog() {
        // 查询所有
        List<DorisCatalog> originList = dorisCatalogService.list();
        Map<String, DorisCatalog> originMap = Maps.uniqueIndex(originList, DorisCatalog::getKeyString);

        // 待同步数据
        List<DorisCatalog> dealList = dorisMetadataRepository.showCatalogs()
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisCatalog.class))
                .toList();
        Map<String, DorisCatalog> dealMap = Maps.uniqueIndex(dealList, DorisCatalog::getKeyString);

        // 新增数据
        List<DorisCatalog> addList = dealList.stream().filter(deal -> !originMap.containsKey(deal.getKeyString())).toList();

        // 移除数据
        List<DorisCatalog> removeList = originList.stream().filter(item -> !dealMap.containsKey(item.getKeyString())).toList();

        // 保存数据
        List<DorisCatalog> saveList = dealList
                .stream()
                .filter(deal -> originMap.containsKey(deal.getKeyString()))
                .peek(deal -> {
                    DorisCatalog origin = originMap.get(deal.getKeyString());
                }).toList();

        dorisCatalogService.addList(addList);
        dorisCatalogService.saveList(saveList);
        dorisCatalogService.deleteList(removeList);

        return dorisCatalogService.list();
    }
}